<?php
namespace Admin\Controller;
class GoodsController extends CommonController {
	private $model;
	
	public function __auto() {
		$this -> model = new \Common\Model\Goods;
	}

	public function index() {
		$listModel=new \Common\Model\GoodsList;
		$listData=$listModel->get();

		$data=$this->model->get();
		foreach ($listData as $k => $v) {
			foreach ($data as $key => $value) {
				if($v['gid']==$value['gid']){
					$data[$key]['gnumber']+=$v['inventory'];
				}
			}
		}
//		p($listData);
//		p($data);
//		die;
		
//		p($data);die;
		View::with('data',$data);
		View::make();
	}

	public function add() {
		//所属分类
		$cateModel = new \Common\Model\Cate;
		$cateData = $cateModel-> get();
		$cateData=Data::tree($cateData,'cname');
		View::with('cateData', $cateData);
		//所属品牌
		$brandModel = new \Common\Model\Brand;
		$brandData = $brandModel -> get();
		View::with('brandData', $brandData);
		//商品属性
		$attrModel = new \Common\Model\TypeAttr;
		$attrData = $attrModel -> get();
		View::with('attrData', $attrData);
		//用户
		$adminModel=new \Admin\Model\Login;
		$adminData=$adminModel->get();
		View::with('adminData',$adminData);
//		p($adminData);die;
		if(IS_POST){
//			p($_POST);die;
			if($this->model->store()){
//				die;
				View::success('添加商品成功',U('Goods/index'));
			}
//			die;
			View::error($this->model->getError());
		}
		View::make();
	}
	//修改信息
	public function edit(){
		$gide=Q('get.gid',0,'intval');
		$tid=Q('get.tid',0,'intval');
//		p($tid);die;
		View::with('gide',$gide);
		//如果提交了修改
		if(IS_POST){
//			p($_POST);die;
			if($this->model->edit($gide)){
//				die;
			View::success('修改成功',U('Goods/index'));
			}
//			die;
			view::error($this->model->getError());
		}
		//获取旧数据
		//商品信息——旧数据
		$goodsData=$this->model->where(array('gid'=>$gide))->find();
		//将商品信息中的列表图信息转换为数组形式以遍历出现在页面中
		$goodsData['pic']=explode(',', $goodsData['pic']);
		View::with('goodsData',$goodsData);
		//商品详细表——旧数据
		$detailModel=new \Common\Model\Detail;
		$detailData=$detailModel->where(array('gid'=>$gide))->find();
		//讲商品详细表信息中的big，转换为数组形式以显示在页面中
		$detailData['big']=explode(',', $detailData['big']);
		$detailData['small']=explode(',', $detailData['small']);
		$detailData['mid']=explode(',', $detailData['mid']);
		View::with('detailData',$detailData);
		//商品属性表——旧数据
		$attrModel=new \Common\Model\GoodsAttr;
		$attrData=$attrModel->where(array('gid'=>$gide))->get();
		//将商品属性表中的属性值存到新建的数组中
		$gtvalue=array();
		foreach ($attrData as $k => $v) {
			$gtvalue['gtvalue'][]=$v['gtvalue'];
		}
		View::with('gtvalue',$gtvalue);
		View::with('attrData',$attrData);
		//分类列表数据-获取cid
		$cateModel=new \Common\Model\Cate;
		$cateData=$cateModel->get();
		$cateData=Data::tree($cateData,'cname');
		View::with('cateData',$cateData);
		//获取用户表-获取aid
		$adminModel=new \admin\Model\Login;
		$adminData=$adminModel->get();
		View::with('adminData',$adminData);
		//获取品牌信息-获取bid
		$brandModel=new \Common\Model\Brand;
		$brandData=$brandModel->get();
		View::with('brandData',$brandData);
		//类型属性表-获取所有属性值和属性名
		$typeModel=new \Common\Model\TypeAttr;
		//获取商品属性一栏；class=0；
		$typeData=$typeModel->where('class=0')->where(array('tid'=>$tid))->get();
		foreach ($typeData as $k => $v) {
			$typeData[$k]['tavalue'] = explode(',', $v['tavalue']);
		}
		//获取商品规格一栏；class=1；
		$typeData1=$typeModel->where('class=1')->where(array('tid'=>$tid))->get();
		foreach ($typeData1 as $k => $v) {
			$typeData1[$k]['tavalue'] = explode(',', $v['tavalue']);
		}
//		$added=	$attrModel->where('added!=0')->where(array('gid'=>$gide))->get();
		//关联商品属性表和
		$aa=$attrModel->join('type_attr','beautiful_goods_attr.taid','=','beautiful_type_attr.taid')->where(array('gid'=>$gide))->where('class=1')->get();
		foreach ($aa as $k => $v) {
			$aa[$k]['tavalue']=explode(',', $v['tavalue']);
		}
		View::with('aa',$aa);
//		p($aa);
//		View::with('added',$added);
//		p($typeData1);
		
//		p($attrData);
//		p($gtvalue);
//		p($detailData);
//		p($goodsData);
//		p($added);
//		die;

		View::with('typeData1',$typeData1);
		View::with('typeData',$typeData);
		View::make();
		
	}
	//删除
	public function del(){
		$gid=Q('get.gid');
		$attrModel=new \Common\Model\GoodsAttr;
		$attrModel->where(array('gid'=>$gid))->delete();
		$this->model->where(array('gid'=>$gid))->delete();
		$detailModel=new \Common\Model\Detail;
		$detailModel->where(array('gid'=>$gid))->delete();
		View::success('刪除成功');
	}

	public function ajaxGetAttr() {
		$tid = $_POST['tid'];
		$attrModel = new \Common\Model\TypeAttr;
		$attrData = $attrModel -> where(array('tid' => $tid)) -> get();
		foreach ($attrData as $k => $v) {
			$attrData[$k]['tavalue'] = explode(',', $v['tavalue']);
		}
//				p($attrData);die;
		View::ajax($attrData);
	}

	public function ajaxUpload() {
		$file = Upload::path("Upload/" . date("ymd")) -> make();
		View::ajax($file[0]);

	}
	 //异步删除图片
	public function ajaxDelImg() {
		if (unlink($_POST['url'])) {
			View::ajax(1);
		} else {
			View::ajax(0);
		}
	}
	
}
?>